<?php
namespace adminv1;
use adminv1\CoreController;
use common\models\AlbumClass;
use common\utils\OutputExecl;

class AlbumClassController extends CoreController
{

	## 列表 
	public function actionList()
	{
		$whereArr = $this->getWhere();
		$params = array(
			'field'	=> ['*'],
			'page'	=> $this->request('page',1),
			'limit'	=> $this->request('page_size',10),
			'order' => 'id asc',
		);

		$data = AlbumClass::dataList($whereArr, $params);
		$totalNums = AlbumClass::$totalNums;
		return $this->success('数据列表', $data, ['totalNums' => $totalNums]);
	}

	## 组装条件
	private function getWhere()
	{
		$where = $and = [];
		$search = $this->request('search');
		$search = $search?json_decode($search,1):'';
		if(!$search) return [];

		foreach($search as $key => $val)
		{
			if(!$val) continue;
			if($key == 'date')
			{
				if(!$val['0'] || !$val['1']) continue;
				$and[] = ['between', 'create_time', strtotime($val[0]), strtotime($val[1])]; 
			}else
			{
				$where[$key] = $val;
			}			
		}
		return ['where' => $where, 'and' => $and];
	}

	## 信息
	public function actionInfo()
	{
		$id = $this->request('id');
		if(!$id) return $this->error('参数有误');
		$data = AlbumClass::find()->where(['id' => $id])->asarray()->one();
		return $this->success('数据', $data);
	}

	## 添加、编辑
	public function actionEdit()
	{
		$id = $this->request('id');
		
		$model = new AlbumClass;
		if($id)
		{
			$model = AlbumClass::findOne(['id' => $id]);
			if(!$model) return $this->error('参数有误');
		}else{
			$this->request['create_time'] = $this->curDateTime;
		}
		$this->request['update_time'] = $this->curDateTime;

		if(!$model->load($this->request, '') || !$model->validate() || !$model->save(false)){
			return $this->error('操作失败');
		}

		return $this->success('操作成功');
	}


	## 删除
	public function actionDel()
	{
		if(!$id = $this->request('id')) $this->error('参数有误');

		$res = AlbumClass::deleteAll(['id'=>$id]);

		if(!$res) return $this->error('删除失败');
		return $this->success('删除成功');
	}


}
